package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Empl;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmplService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmplService emplService;

    //分页查询
    @GetMapping("/empl")
    public Result select(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "2")Integer pagesize){
        //使用@RequestParam中的defaultValue属性,可以为参数设定默认值
       /* if (page==null)
            page=1;
        if (pagesize==null)
            pagesize=3;*/
        PageBean pageBean=emplService.select(page,pagesize);
        return  Result.success(pageBean);
    }
    //分页条件查询
    @GetMapping("/empls")
    public Result selectByChoice(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "2")Integer pagesize,
                                 String name, Short gender,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("执行查询,{},{},{},{},{},{}",page,pagesize,name,gender,begin,end);
        PageBean pageBean=emplService.selectByChoice(page,pagesize,name,gender,begin,end);
        return  Result.success(pageBean);
    }

    //删除员工
    @Log
    @DeleteMapping("/empls/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除:{}",ids.toString());
        emplService.delete(ids);
        return Result.success();
    }

    //新增员工 insert into emp (username,name,gender,image,job,entrydate,dept_id,create_time,update_time) values("xyw","月薇","2","2.png",3,"2023-6-6",4,"2023-10-10","2023-10-10");
    @PostMapping("/empls")
    @Log
    public Result insert(@RequestBody Empl empl){
        log.info("信息:{}",empl.toString());
        emplService.insert(empl);
        return Result.success();
    }

    //根据id查询员工
    @GetMapping("/empls/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询id:{}",id);
        Empl empl=emplService.selectById(id);
        return Result.success(empl);
    }
    //修改员工信息 update emp set username=#{username},name=#{name},gender=#{gender},image=#{image},job=#{job},entrydate=#{entrydate},dept_id=#{deptId},create_time=#{createTime},update_time=#{updateTime} where id=#{id}
    @Log
    @PutMapping("/empls")
    public  Result update(@RequestBody Empl empl){
        log.info("查询信息:{}",empl.toString());
        emplService.update(empl);
        return Result.success();
    }
}
